/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Aptech.DAL.MgLands;
import Aptech.DALInterface.LandsInterface;
import Aptech.Service.SV_Lands;
import Form.LandsForm;
import HibernateMappingPoJo.Lands;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author nkhoan
 */
public class LandsAction extends DispatchAction {

    private SV_Lands mgLand = new SV_Lands();

    public ActionForward getAllLand(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        LandsForm landForm = (LandsForm) form;
        landForm.setArrLands(mgLand.getAllLand());
        request.setAttribute("sitemap", "List of Land");
        return map.findForward("list");

    }

    public ActionForward addLand(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        LandsForm landForm = (LandsForm) form;
        String type = request.getParameter("type");
        request.setAttribute("sitemap", "Add A New Land");
        if (type == null) {
            request.setAttribute("type", "add");
            return map.findForward("edit");
        } else {
            UUID id = UUID.randomUUID();
            Lands land = new Lands();
            land.setName(landForm.getName());
            land.setAddress(landForm.getAddress());

            if (landForm.getBuildingPermit() == null) {
                land.setBuildingPermit(Boolean.FALSE);
            } else {
                land.setBuildingPermit(landForm.getBuildingPermit());
            }

            if (landForm.getOccupancyPermit() == null) {
                land.setOccupancyPermit(Boolean.FALSE);
            } else {
                land.setOccupancyPermit(landForm.getOccupancyPermit());
            }

            land.setSquare(landForm.getSquare());
            land.setId(id.toString());

            mgLand.addLands(land);
            request.setAttribute("save", "save");
        }

        return map.findForward("edit");
    }

    public ActionForward editLand(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        LandsForm landForm = (LandsForm) form;
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        request.setAttribute("sitemap", "Edit A Land");
        if (type.equals("edit1")) {
            request.setAttribute("id", id);
            request.setAttribute("type", type);
            // Set value for Form
            Lands land = mgLand.getLandByID(id);
            landForm.setAddress(land.getAddress());            
            landForm.setName(land.getName());
            landForm.setNote(land.getName());          
            landForm.setSquare(land.getSquare());

            if (land.getBuildingPermit() == Boolean.FALSE) {
                request.setAttribute("BuildingPermit", "0");
            } else {
                request.setAttribute("BuildingPermit", "1");
            }

            if (land.getOccupancyPermit() == Boolean.FALSE) {
                request.setAttribute("OccupancyPermit", "0");
            } else {
                request.setAttribute("OccupancyPermit", "1");
            }

            return map.findForward("edit");
        } else {
            Lands land = new Lands();
            land.setId(id);
            land.setName(landForm.getName());
            land.setAddress(landForm.getAddress());
             if (landForm.getBuildingPermit() == null) {
                land.setBuildingPermit(Boolean.FALSE);
            } else {
                land.setBuildingPermit(landForm.getBuildingPermit());
            }

            if (landForm.getOccupancyPermit() == null) {
                land.setOccupancyPermit(Boolean.FALSE);
            } else {
                land.setOccupancyPermit(landForm.getOccupancyPermit());
            }
            land.setSquare(landForm.getSquare());
            mgLand.editLands(land);
            request.setAttribute("save", "save");
        }


        return map.findForward("edit");

    }

    public ActionForward deleteLand(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        mgLand.deleteLands(id);
        response.sendRedirect("/NTB/land.do?method=getAllLand");

        return map.findForward("list");

    }

    public ActionForward detailLand(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception{

        LandsForm landForm = (LandsForm) form;
        String id = request.getParameter("id");
        request.setAttribute("id", id);
        request.setAttribute("type", "detail");
        request.setAttribute("sitemap", "Information Detail Of A Land");
            // Set value for Formpe
            Lands land = mgLand.getLandByID(id);
            landForm.setAddress(land.getAddress());
            landForm.setName(land.getName());
            landForm.setNote(land.getName());
            landForm.setSquare(land.getSquare());

      if (land.getBuildingPermit() == Boolean.FALSE) {
                request.setAttribute("BuildingPermit", "0");
            } else {
                request.setAttribute("BuildingPermit", "1");
            }

            if (land.getOccupancyPermit() == Boolean.FALSE) {
                request.setAttribute("OccupancyPermit", "0");
            } else {
                request.setAttribute("OccupancyPermit", "1");
            }
            return map.findForward("edit");
    }
}
